<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\library\Upload;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 示例接口
 */
class Kuaidi extends Api
{


    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    public function getinfobyid(){


        $id=$this->request->param('id');

        $list=Db::name('aa_kuaidi')->where(['id'=>$id])->find();

        $list['img']=$list['img']?explode(',',$list['img']):[];

        $this->success('',$list);
    }

    /**
     *Telegram @cocolive9 2023/6/22 22:24
     * 添加快递信息
     */
    public function applykuaidi(){

        $info=$this->request->param();
        $user=$this->auth->getUser();

        if (intval($info['id'])>0){

            $id=$info['id'];
            unset($info['id']);


            Db::name('aa_kuaidi')->where(['id'=>$id])->update($info);

        }else {

            unset($info['id']);

            $info['time']=time();
            $info['user']=$user['nickname'];
            $info['uid']=$user['id'];

            Db::name('aa_kuaidi')->data($info)->insert();


        }

        $this->success('成功!');



    }

    /*
     * 全部出库单
     * */

    public function getallfile(){

        $page=$this->request->param('page',1);


        $limit=($page-1)*100 . ',100';


        $list=Db::name('aa_kuaidi')->order('id desc ')->limit($limit)->select();


        foreach ($list as &$key){

            $key['time']=date('Y-m-d H:i',$key['time']);

            $count=Db::name('aa_kuaidi_log')->where(['kid'=>$key['id']])->count();
            $key['count']=$count;

        }


        $this->success('出库单',$list);

    }


    /*
     * 商品详情
     * */

    public function getgoodsbyid(){

        $code=$this->request->param('code');

        $find=Db::name('newphoto')->where('code|sku','eq',$code)
            ->where('typein','eq',1)
            ->where('typeout','eq',0)
            ->find();

        if ($find){
            $this->success('详情',$find);

        }else{
            $this->error('未找到相关订单');

        }
    }

    public  function chuku(){

        $id=$this->request->param('id');
        $kid=$this->request->param('kid');

        Db::name('newphoto')->where(['id'=>$id])->update(['typeout'=>1]);

        $user=$this->auth->getUser();

        $kd=Db::name('aa_kuaidi')->where(['id'=>$kid])->find();


        $logs['name']=$user['nickname'];
        $logs['desc']='修改状态为:出库,快递单号：'.$kd['order'];
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];
        $info=Db::name('newphoto')->where(['id'=>$id])->find();
        $logs['cid']=$info['caseid'];

        Db::name('aa_photolpg_log')->data($logs)->insert();



        $logs1['order']=$id;
        $logs1['time']=date('Y-m-d H:i');
        $logs1['kid']=$kid;
        $logs1['user']=$user['nickname'];

        Db::name('aa_kuaidi_log')->data($logs1)->insert();

        $this->success('操作成功');

    }

    public function getalllistbykid(){

        $id=$this->request->param('kid');

        $list= Db::name('aa_kuaidi_log')->where(['kid'=>$id])->select();

        foreach ($list as &$key){
            $log=Db::name('newphoto')->where(['id'=>$key['order']])->find();

            $key['title']= $log['code'];
        }


        $this->success('操作成功',$list);
    }


    public function backgoodtocode(){

        $id=$this->request->param('id');

        $list= Db::name('aa_kuaidi_log')->where(['id'=>$id])->find();

        Db::name('newphoto')->where(['id'=>$list['order']])->update(['typeout'=>1]);


        Db::name('aa_kuaidi_log')->where(['id'=>$id])->delete();

        $this->success('操作成功',$list);
    }


    public function deletesme(){

        $id=$this->request->param('id');

        Db::name('aa_kuaidi')->where(['id'=>$id])->delete();

        $this->success('操作成功');
    }

    /**
     * @return c查询信息
     */
    public function getalllistbycode(){

        $code=$this->request->param('code');

        $list= Db::name('aa_kuaidi_log')
            ->alias('a')
            ->join('newphoto b','a.order=b.id')
            ->field('a.*,b.title,b.code')
            ->where('b.code','like','%'.$code.'%')
            ->order('b.id desc')
            ->limit(99)
            ->select();

        $ret=Db::getLastSql();


        $this->success('操作成功'.$ret,$list);


    }

    public function getkuaidbykid(){

        $code=$this->request->param('id');

        $list= Db::name('aa_kuaidi')
            ->where('id','eq',$code)
            ->find();


        $list['imgs']=explode(',',$list['img']);

        $ret=Db::getLastSql();


        $this->success('操作成功1'.$ret,$list);

    }





}